package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Diary;
import com.example.domain.entity.LoginUser;
import com.example.domain.entity.Plant;
import com.example.domain.entity.User;
import com.example.exception.FloraException;
import com.example.mapper.DiaryMapper;
import com.example.service.DiaryService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Diary)表服务实现类
 *
 * @author makejava
 * @since 2022-11-30 16:38:58
 */
@Service
public class DiaryServiceImpl extends ServiceImpl<DiaryMapper, Diary> implements DiaryService {
    /** 显示/查询 植物日记（按日期降序排列） */
    @Override
    public List<Diary> getDiaryByPlantId(Long plantId) {
        QueryWrapper<Diary> qw=new QueryWrapper<>();
        qw.eq("plant_id",plantId);
        return this.baseMapper.selectList(qw);
    }
    @Override
    public IPage<Diary> findDiary(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        Page<Diary> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Diary> qw = new LambdaQueryWrapper<>();
        qw.eq(Diary::getPlantId,condition.get("plant_id"));
        qw.le(condition.containsKey("create_time"),Diary::getCreateTime,condition.get("create_time"));
        qw.like(condition.containsKey("state"),Diary::getState,condition.get("state"));
        qw.like(condition.containsKey("title"),Diary::getTitle,condition.get("title"));
        qw.orderByDesc(Diary::getCreateTime);

        return page(page,qw);
    }

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public Diary addDiary(Diary diary) throws FloraException {
//        User user = ((LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
//        /** 先判断该用户新增的日记是否是自己的 */
//        if(!diary.getCreateBy().equals(user.getId())&&user.getType()=="0"){
//            throw new FloraException("无权限添加非本用户的植物日记");
//        }
        try{
            this.baseMapper.insert(diary);
            return diary;
        }
        catch (Exception e){
            throw new FloraException("写植物日记失败"+e.getMessage());
        }
    }

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public void deleteDiary(Long id) throws FloraException {
        /** 先查找日记id，若不存在，抛出异常 */
        Diary diary = this.baseMapper.selectById(id);
        if(Objects.isNull(diary)){
            throw new FloraException("要删除的日记不存在");
        }
//        /** 再验证是否是本人操作 */
//        User user = ((LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
//        if(!diary.getCreateBy().equals(user.getId())&&user.getType()=="0"){
//            throw new FloraException("无权限删除非本人的植物日记");
//        }
        try {
            this.baseMapper.deleteById(id);
        } catch (Exception e) {
            throw new FloraException("删除植物日记失败");
        }
    }

    @Override
    //@Transactional(rollbackFor = Exception.class)
    public void updateDiary(Diary diary) throws FloraException {
        if(this.baseMapper.selectById(diary.getId())==null){
            throw new FloraException("要修改的文章不存在");
        }
//        User user = ((LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
//        if(!diary.getCreateBy().equals(user.getId())&&user.getType()=="0"){
//            throw new FloraException("无权限修改非本人的植物日记");
//        }

        try {
            this.baseMapper.updateById(diary);
        } catch (Exception e) {
            throw new FloraException("修改植物日记失败");
        }
    }

//    @Override
//    public List<Diary> gerDiaryByPlantId(Long plantId) {
//        LambdaQueryWrapper<Diary> qw = new LambdaQueryWrapper<>();
//
//    }
}

